<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>随机色插件</title>
		<style type = "text/css">
			*{
				margin:0;
				padding:0;
			}
			
			div{
				width:100px;
				height:100px;
				border:2px solid black;
				margin: 80px;
				float:left;
				text-align:center;
				line-height:100px;
				font-size:48px;
				color:white;
			}
		</style>
		<script type = "text/javascript" src = "jquery-3.4.1.js"></script>
	</head>
	<body>
		<div>1</div>
		<div>2</div>
		<div>3</div>
		<div>4</div>
		<div>5</div>
		<script>
			(function($){//使用立即执行函数传参数，解决命名冲突的问题
			
				$.fn.extend({//使用$.fn.extend(构造插件d)
				
					randomColor:function(){
						function random(){
							var r = Math.floor(Math.random() * 256);
							var g = Math.floor(Math.random() * 256);
							var b = Math.floor(Math.random() * 256);
							
							return "rgb("+r+","+g+","+b+")"
						}
						
						this.each(function(index){//使用this.each()遍历jQuery对象
							$(this).css({
								backgroundColor:random()
							})
						})
						
						return this;//返回jQuery对象
					}
				})
				
			})(jQuery);
			
			$("div").randomColor();
		</script>
	</body>
</html>
